package com.yachang.qym.usbhost.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.Toast;

import com.yachang.qym.usbhost.R;
import com.yachang.qym.usbhost.adapter.ContactAdapter;
import com.yachang.qym.usbhost.model.CalledModel;
import com.yachang.qym.usbhost.model.ContactModel;
import com.yachang.qym.usbhost.utils.AllData;

import java.util.ArrayList;
import java.util.List;

public class CallerActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private ListView mLvContact;
    private ContactAdapter mAdapter;
    private List<ContactModel> contactModels = new ArrayList<>();
    private CalledModel calledModels;
    private int calledID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_caller);
        Intent intent = getIntent();
        calledID = intent.getIntExtra("calledID", 0) - 1;
        if(calledID > AllData.calledModels.size()){
            Toast.makeText(this, "当前信道无接受联系人！",Toast.LENGTH_SHORT).show();
            mToolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }else {
            initView();
        }

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("接收组列表");
        mLvContact = (ListView) findViewById(R.id.lv_contact);
        if(calledID - 1 > 0 && calledID - 1 < AllData.calledModels.size()) {
            calledModels = AllData.calledModels.get(calledID - 1);
            if (calledModels != null) {
                for (int i = 0; i < calledModels.getCalledSize(); i++) {
                    for (int j = 0; j < AllData.contactModels.size(); j++) {
                        if (AllData.contactModels.get(j).getContactID() == calledModels.getContactID().get(i).getContactID()) {
                            contactModels.add(AllData.contactModels.get(j));
                            break;
                        }
                    }
                }
                getSupportActionBar().setTitle(calledModels.getName());
                if (contactModels != null) {
                    mAdapter = new ContactAdapter(this, contactModels);
                    mLvContact.setAdapter(mAdapter);
                }
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home :
                CallerActivity.this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
